using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
//using System.Runtime.InteropServices;


namespace Pongster
{
    public class InputHandler : GameComponent
    {
        static MouseState mouseState;
        static MouseState lastMouseState;
        static KeyboardState keyboardState;
        static KeyboardState lastKeyboardState;

        static Vector2 mousePosition;

        public static Vector2 RawMousePosition { get { return new Vector2(mouseState.X - Settings.xOffset, mouseState.Y - Settings.yOffset); } }

        public static Vector2 MousePosition
        {
            get {                
                mousePosition = Vector2.Transform(RawMousePosition, Matrix.Invert(Resolution._ScaleMatrix));
                //Clamp the custom cursor
                //mousePosition.X = MathHelper.Clamp(mousePosition.X, 0, Settings.basescreenWidth);
                //mousePosition.Y = MathHelper.Clamp(mousePosition.Y, 0, Settings.basescreenHeight);
                return mousePosition; 
            }
        }

        public static Point MousePoint
        {
            get{ return new Point((int)MousePosition.X, (int)MousePosition.Y); }
        }

        public static bool MouseInactive
        {
            get { return mouseState == lastMouseState; }
        }

        public static KeyboardState KeyboardState
        {
            get { return keyboardState; }
        }

        public static KeyboardState LastKeyboardState
        {
            get { return lastKeyboardState; }
        }

        //Constructor
        public InputHandler(Game game)
            : base(game)
        {
            keyboardState = Keyboard.GetState();
            mouseState = Mouse.GetState();
        }


        public override void Initialize()
        {
            //Mouse.SetPosition(Settings.basescreenWidth / 2, Settings.basescreenHeight / 2);

            base.Initialize();
        }


        public override void Update(GameTime gameTime)
        {
            lastKeyboardState = keyboardState;
            keyboardState = Keyboard.GetState();

            lastMouseState = mouseState;
            mouseState = Mouse.GetState();

            #region Clamp the mouse to the Window
            //Stop the mouse from leaving the Window
            //Rectangle rect = Game.Window.ClientBounds; 
            //rect.Width += rect.X - Settings.xOffset;
            //rect.X += Settings.xOffset;
            //rect.Height += rect.Y - Settings.yOffset;
            //rect.Y += Settings.yOffset;
            //ClipCursor(ref rect);
            #endregion


            base.Update(gameTime);
        }

        //[DllImport("user32.dll")]
        //static extern void ClipCursor(ref Rectangle rect);


        //Methods
        public static void FlushInput()
        {
            lastKeyboardState = keyboardState;
        }

        public static bool KeyReleased(Keys key)
        {
            return keyboardState.IsKeyUp(key) && lastKeyboardState.IsKeyDown(key);
        }

        public static bool KeyPressed(Keys key)
        {
            return keyboardState.IsKeyDown(key) && lastKeyboardState.IsKeyUp(key);
        }

        public static bool KeyDown(Keys key)
        {
            return keyboardState.IsKeyDown(key);
        }

        #region Mouse Controls
        public static bool LeftMouseClick()
        {
            return lastMouseState.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed;
        }

        public static bool RightMouseClick()
        {
            return lastMouseState.RightButton == ButtonState.Released && mouseState.RightButton == ButtonState.Pressed;
        }

        public static bool MiddleMouseClick()
        {
            return lastMouseState.MiddleButton == ButtonState.Released && mouseState.MiddleButton == ButtonState.Pressed;
        }

        public static bool LeftMouseReleased()
        {
            return lastMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released;
        }

        public static bool RightMouseReleased()
        {
            return lastMouseState.RightButton == ButtonState.Pressed && mouseState.RightButton == ButtonState.Released;
        }

        public static bool LeftMouseHold()
        {
            return mouseState.LeftButton == ButtonState.Pressed;
        }

        public static bool RightMouseHold()
        {
            return mouseState.RightButton == ButtonState.Pressed;
        }

        #endregion

        private static Vector2 GetMouseAimDirection(Vector2 position)
        {
            Vector2 direction = MousePosition - position;

            if (direction == Vector2.Zero)
                return Vector2.Zero;
            else
                return Vector2.Normalize(direction);
        }
        
    }
}
